package com.sdwz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import com.sdwz.commonutil.exception.LibraryException;
import com.sdwz.user.entity.BorrowCardInfo;
import com.sdwz.user.entity.StuInfo;
import com.sdwz.user.entity.UserInfo;
import com.sdwz.user.entity.vo.BorrowCardInfoVo;
import com.sdwz.user.mapper.BorrowCardInfoMapper;
import com.sdwz.user.service.BorrowCardInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdwz.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 借阅卡信息表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-28
 */
@Service
public class BorrowCardInfoServiceImpl extends ServiceImpl<BorrowCardInfoMapper, BorrowCardInfo> implements BorrowCardInfoService {

    @Autowired
    UserInfoService userInfoService;
    /**
     * 根据条件分页查询，获得id集合
     * @param current
     * @param limit
     * @param borrowCardInfoVo
     * @return
     */
    @Override
    public List<String> getBorrowCardInfoIdList(long current, long limit, BorrowCardInfoVo borrowCardInfoVo) {

        return null;
    }

    /**
     * 更改借阅卡状态
     * @param userId
     * @param status
     */
    @Override
    public void updateCardStatus(String userId, Integer status) {
        UserInfo userInfo = userInfoService.getById(userId);
        if (userInfo == null){
            throw new LibraryException(201,"当前用户不存在");
        }

        BorrowCardInfo borrowCardInfo = baseMapper.selectOne(new QueryWrapper<BorrowCardInfo>().eq("user_id", userId));
        if (borrowCardInfo == null || borrowCardInfo.getAuthStatus() == 0||borrowCardInfo.getAuthStatus() == 1||borrowCardInfo.getAuthStatus() == -1){
            throw new LibraryException(201,"当前借阅卡不存在");
        }

        if (status==1){
            userInfo.setStatus(2);
            userInfoService.updateById(userInfo);
            borrowCardInfo.setStatus(1);
            baseMapper.updateById(borrowCardInfo);
        }else{
            userInfo.setStatus(-2);
            userInfoService.updateById(userInfo);
            borrowCardInfo.setStatus(0);
            baseMapper.updateById(borrowCardInfo);
        }
    }
}
